<template>
  <div class="container-layer-tag">
    <div
      v-for="(temp,index) in computList"
      v-if="showAll || temp.checked "
      :id="temp.id"
      :key="index"
      :miva-test="temp.checked"
      class="tag"
      :class="temp.checked ? `active` : `desabled`"
      @click="checkedEvent(temp)"
    >{{ temp.name }}</div>
  </div>
</template>

<script>
/* eslint-disable */
// api
import { aihao } from '@/api/gain'
export default {
  props: {
    canCheck: {
      type: Boolean,
      default: false
    },
    showAll: {
      type: Boolean,
      default: false
    },
    // eslint-disable-next-line
    checked: {
      // type: any,
      default() {
        const array = []
        return array
      }
    }
  },
  data() {
    return {
      taglist: []
    }
  },
  computed: {
    computList() {
      const array = [...this.taglist]
      for (let i = 0; i < this.taglist.length; i++) {
        const checked = this.checked.indexOf(i.toString()) !== -1
        array[i]['id'] = i
        array[i]['checked'] = checked
      }
      return array
    }
  },
  async created() {
    const res = await aihao()
    if (res.code === 1) {
      this.taglist = res.data
    }
  },
  methods: {
    checkedEvent(temp) {
      console.log(this.checked)
      if (this.canCheck === false) {
        return console.log(`未开启选择`)
      }
      if (temp.checked) {
        temp.checked = false
        const index = this.checked.indexOf(temp.id.toString())
        this.checked.splice(index, 1)
        return
      }
      if (this.checked.length >= 5) return console.log(`不能再选了`)
      temp.checked = true
      this.checked.push(temp.id.toString())
    }
  }
}
</script>

<style lang="less">
.container-layer-tag {
  width: 90%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-bottom: 5%;
  .tag {
    width: 20%;
    padding: 5% / 2 5% / 4;
    margin: 5% / 4;
    display: inline-block;
    border-radius: 30px;
    text-align: center;
    font-size: 0.875rem;
    white-space: nowrap;
    background-color: #6392EA;
    color: #fff;
    &.desabled {
      background-color: #eeeeee;
      color: #666;
    }
  }
}
</style>
<style scoped>
.tag :first-child{
  background: #6392EA;
}
.tag :nth-child(2){
  background: #EF83B9;
}
</style>
